﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ProductMonitor.UserControls
{
    /// <summary>
    /// RingUC.xaml 的交互逻辑
    /// </summary>
    public partial class RingUC : UserControl
    {
        public RingUC()
        {
            InitializeComponent();
            SizeChanged += RingUC_SizeChanged; // 订阅SizeChanged事件
        }
        // 当控件大小发生变化时，重新计算圆环的半径
        private void RingUC_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Drug(PercentValue);// 调用Drug方法更新圆环
        }

        //定义依赖属性
        public double PercentValue
        {
            get { return (double)GetValue(PercentValueProperty); }
            set { SetValue(PercentValueProperty, value); }
        }

        // Using a DependencyProperty as the backing store for PercentValue.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty PercentValueProperty =
            DependencyProperty.Register("PercentValue", typeof(double), typeof(RingUC));


        //圆环方法
        private void Drug(double percent)
        {
            LayoutGrid.Width = Math.Min(RenderSize.Width, RenderSize.Height); // 设置LayoutGrid的宽度为RenderSize的最小值
            // 确保百分比在0到100之间
            percent = Math.Max(0, Math.Min(100, percent));
            // 计算圆环的角度
            double angle = 360 * (percent / 100);
           // 计算圆环的半径
            double radius = LayoutGrid.Width / 2 - 5; // 减去10是为了留出边距
            // 计算圆环的路径
            PathFigure pathFigure = new PathFigure
            {
                StartPoint = new Point(ActualWidth / 2, ActualHeight / 2 - radius), // 圆心上方的点
                IsClosed = false
            };
            // 添加弧线到路径中
            pathFigure.Segments.Add(new ArcSegment
            {
                Point = new Point(ActualWidth / 2 + radius * Math.Sin(angle * Math.PI / 180), ActualHeight / 2 - radius * Math.Cos(angle * Math.PI / 180)),
                Size = new Size(radius, radius),
                SweepDirection = SweepDirection.Clockwise,
                IsLargeArc = angle > 180 // 如果角度大于180度，则为大弧线
            });
            PathGeometry pathGeometry = new PathGeometry();
            pathGeometry.Figures.Add(pathFigure);
            RingPath.Data = pathGeometry; // 更新圆环路径数据

        }

    }
}
